package com.eagle.rate.common.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	public static String getStringyyyy_MM_dd() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String da = format.format(cal.getTime());
		return da;
	}

	/**
	 * 
	 * @param date
	 * @return 字符串yyyy-MM-dd
	 */
	public static Date parseyyyy_MM_dd(String date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date2 = null;
		try {
			date2 = format.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return new Date();
		}
		if (date2 != null) {
			return date2;
		}
		return null;
	}

	public static String getStringyyyy_MM_dd_HH_mm_ss() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String da = format.format(cal.getTime());
		return da;
	}

	public static Date parseyyyy_MM_dd_HH_mm_ss(String str) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = format.parse(str);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new Date();
		}
		return date;
	}

	public static String formatyyyy_MM_dd_HH_mm_ss(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return format.format(date);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		}
		return "0000-00-00 00:00:00";
	}

	/**
	 * @return 字符串yyyyMMddHHmmss
	 */
	public static String getStringyyyyMMddHHmmss() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		String da = format.format(cal.getTime());
		return da;
	}

	/**
	 * 
	 * @param fullDate
	 * @return
	 */
	public static Date ParseyyyyMMddHHmmss(String fullDate) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		try {
			return format.parse(fullDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new Date();
		}
	}

	public static String getDateFormat(Date date, String cformat) {
		if (date == null)
			return "";
		SimpleDateFormat format = new SimpleDateFormat(cformat);// YYYYMMDDHHmmss
		String da = format.format(date);
		return da;
	}

	/**
	 * 返回日期
	 * 
	 * @return
	 */
	public static String getStringyyyyMMdd() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");// YYYYMMDD
		String da = format.format(cal.getTime());
		return da;
	}

	/**
	 * 解释日期
	 * 
	 * @return
	 */
	public static String parseyyyymmdd(String yyyymmdd) {
		if ("00000000".equals(yyyymmdd)) {
			return "";
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");// YYYYMMDD
		SimpleDateFormat format2 = new SimpleDateFormat("yyyy年MM月dd日");// YYYYMMDD
		try {
			return format2.format(format.parse(yyyymmdd));
		} catch (ParseException e) {
			e.printStackTrace();
			return format2.format((new Date()));
		}
	}

	/**
	 * 返回时间 格式 Hhmmss
	 * 
	 * @param args
	 */

	public static String getStringHhmmss() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("HHmmss");// YYYYMMDD
		String da = format.format(cal.getTime());
		return da;
	}

	/**
	 * 获得当前月的开始时间
	 * 
	 * @return
	 */
	public static Date getCurrentMonth() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return cal.getTime();
	}

	/**
	 * 获得当天的时间
	 * 
	 * 
	 */

	public static Date getCurrentDate() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return cal.getTime();
	}

	/**
	 * 根据天数返回N天前的时间
	 * 
	 */
	public static Date getAddDays(int days) {
		Calendar cal = Calendar.getInstance();
		cal.setLenient(true);
		cal.add(Calendar.DAY_OF_MONTH, days);
		return cal.getTime();

	}

	/**
	 * 根据天数返回给定时间N天前的时间
	 * 
	 */
	public static Date getAddDays(Date startDate, int days) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(startDate == null ? new Date() : startDate);
		cal.add(Calendar.DAY_OF_MONTH, days);
		return cal.getTime();
	}

	/**
	 * 根据天数返回N分钟前的时间
	 * 
	 */
	public static Date getAddMin(int min) {
		Calendar cal = Calendar.getInstance();
		cal.setLenient(true);
		cal.add(Calendar.MINUTE, min);
		return cal.getTime();

	}

	/**
	 * yyyyMMddHHmmss---->yyyy-MM-dd hh:mm:ss
	 * 
	 */

	public static String ChangeFormat(String str_date) {
		SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMddHHmmss");
		SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		if (str_date == null || "".equals(str_date)) {
			return format2.format(new Date());
		}
		Date date = null;
		try {
			date = format1.parse(str_date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return format2.format(date);
	}

	/**
	 * 两个日期相减,得到天数
	 * 
	 * @param args
	 */

	public static long dayMinus(Date startDate, Date endDate) {
		long startTime = startDate.getTime();
		long endTime = endDate.getTime();
		return (endTime - startTime) / (24 * 60 * 60 * 1000) > 0 ? (endTime - startTime)
				/ (24 * 60 * 60 * 1000)
				: (startTime - endTime) / (24 * 60 * 60 * 1000);
	}

	public static int[] dayCompute(Date from, Date to) {
		int ONE_DAY = 24 * 60 * 60 * 1000;
		int ONE_HOUR = 60 * 60 * 1000;
		int ONE_MIN = 60 * 1000;
		int[] ret = new int[3];
		long s = from.getTime();
		long t = to.getTime();
		long dis = t - s;
		int days = new Long(dis / ONE_DAY).intValue();
		int hours = new Long((dis - days * ONE_DAY) / ONE_HOUR).intValue();
		int mins = new Long((dis - days * ONE_DAY - hours * ONE_HOUR) / ONE_MIN)
				.intValue();
		ret[0] = days;
		ret[1] = hours;
		ret[2] = mins;
		return ret;
	}

	/**
	 * 
	 * @todo:判断时间date1是否在时间date2之前 时间格式 2008-08-08 16:16:34
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean isDateBefore(String date1, String date2) {
		try {
			DateFormat df = DateFormat.getDateTimeInstance();
			return df.parse(date1).before(df.parse(date2));
		} catch (ParseException e) {
			// System.out.print("[SYS] " + e.getMessage());
			return false;
		}
	}

	/**
	 * @todo:判断当前时间是否在时间date2之前 时间格式 2005-4-21 16:16:34
	 * @param date2
	 * @return
	 */
	public static boolean isDateBefore(String date2) {
		try {
			Date date1 = new Date();
			DateFormat df = DateFormat.getDateTimeInstance();
			return date1.before(df.parse(date2));
		} catch (ParseException e) {
			System.out.print("[SYS] " + e.getMessage());
			return false;
		}
	}

	/**
	 * 
	 * @todo timestamp转化为 yyyy-MM-dd HH:mm:ss的字符串
	 * @param
	 * @return
	 * @param timestamp
	 * @return
	 */
	public static String timestampToString(Timestamp timestamp) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 定义格式，不显示毫秒
		String str = df.format(timestamp);
		return str;
	}

	/**
	 * 
	 * @todo  Date转化为Timestamp
	 * @param 
	 * @return
	 * @param date
	 * @return
	 */
	public static Timestamp DateToTimestamp(Date date) {
		SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = df1.format(date);
		Timestamp ts = Timestamp.valueOf(time);
		return ts;
	}

	public static void main(String args[]) {
		// Timestamp k = new Timestamp();

		System.out.println(new Timestamp(DateUtil.getCurrentDate().getTime()));

		// System.out.println(getFullDate());
		// System.out.println(getDate());
		// System.out.println(getTime());

		// SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd
		// hh:mm:ss");
		// Date date = null;
		// try {
		// date = format.parse("2008-06-11 12:41:12");
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		// System.out.println(format.format(date));

		// Calendar cal = Calendar.getInstance();
		// int moth = cal.get(Calendar.MONTH);
		// cal.set(Calendar.MONTH, moth - 1);

		// Calendar cal = Calendar.getInstance();
		// cal.set(Calendar.DAY_OF_MONTH, 1);
		// cal.set(Calendar.HOUR_OF_DAY, 0);
		// cal.set(Calendar.MINUTE, 0);
		// cal.set(Calendar.SECOND, 0);
		// Date now = new Date();
		// System.out.println(dayMinus(cal.getTime(), now));
		// System.out.println(getDateInWellFormat());
		// String from="2009-01-13 00:00:00";
		// String to="2009-01-14 01:05:00";
		//
		// //long ms=1232272919781l;
		//
		// int
		// ret[]=dayCompute(parseDateInWellFormat(from),parseDateInWellFormat(to));
		// System.out.println(DateUtil.getDateInWellFormat(DateUtil
		// .getAddDays(-10)));
	}
}
